﻿using System;
using XCI.Sys.Model;
using XCI.Core;
using XCI.Windows.Extensions;
using System.Threading;

namespace XCI.Sys.Service
{
    public class FileUploadService : BaseService<SysApp>
    {
        public static readonly FileUploadService Instance = new FileUploadService();
        protected override string RootUrl => "/api/sys/fileUpload/";

        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="folder">服务端文件夹(可为空,默认为当前日期)</param>
        /// <param name="fileName">文件保存名称(可为空,默认为自动生成)</param>
        /// <param name="displayName">文件显示名称</param>
        /// <param name="localPath">待上传的本地文件路径</param>
        /// <param name="progress">进度报告</param>
        /// <param name="cancellationTokenSource">取消标记</param>
        public FileModel UploadFile(string folder, string fileName, string displayName, string localPath,
            Action<HttpProgressArgs> progress = null, CancellationTokenSource cancellationTokenSource = null)
        {
            var url = GetUrl("upload");
            var map = Map.New.Add("folder", folder).Add("name", fileName);
            return SysRuntime.Api.UploadFile<FileModel>(url, displayName, localPath, map, progress, cancellationTokenSource).EnsureSuccess().Data;
        }

        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="folder">服务端文件夹(可为空,默认为当前日期)</param>
        /// <param name="fileName">文件保存名称(可为空,默认为自动生成)</param>
        /// <param name="displayName">文件显示名称</param>
        /// <param name="localPath">待上传的本地文件路径</param>
        /// <param name="progress">进度报告</param>
        /// <param name="cancellationTokenSource">取消标记</param>
        public BoolMessage<FileModel> UploadFileAsMessage(string folder, string fileName, string displayName, string localPath,
            Action<HttpProgressArgs> progress = null, CancellationTokenSource cancellationTokenSource = null)
        {
            var url = GetUrl("upload");
            var map = Map.New.Add("folder", folder).Add("name", fileName);
            return SysRuntime.Api.UploadFile<FileModel>(url, displayName, localPath, map, progress, cancellationTokenSource);
        }

        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="folder">服务端文件夹(可为空,默认为当前日期)</param>
        /// <param name="fileName">文件保存名称(可为空,默认为自动生成)</param>
        /// <param name="displayName">文件显示名称</param>
        /// <param name="fileData">待上传的二进制</param>
        public FileModel UploadByte(string folder, string fileName, string displayName, byte[] fileData)
        {
            var url = GetUrl("upload");
            var map = Map.New.Add("folder", folder).Add("name", fileName);
            return SysRuntime.Api.UploadByte<FileModel>(url, displayName, fileData, map).EnsureSuccess().Data;
        }

        /// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="serverPath">服务端文件路径</param>
        /// <param name="md5">文件md5</param>
        public byte[] Download(string serverPath, string md5)
        {
            var url = GetUrl("download");
            var map = Map.New.Add("path", serverPath).Add("md5", md5);
            return SysRuntime.Api.DownloadPost(url, map).EnsureSuccess().Data;
        }

        /// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="serverPath">服务端文件路径</param>
        /// <param name="localPath">本地存储文件路径</param>
        /// <param name="md5">文件md5</param>
        /// <param name="cancellationTokenSource">取消标记</param>
        /// <param name="progress">进度报告</param>
        public BoolMessage Download(string serverPath, string localPath, string md5,
            Action<HttpProgressArgs> progress = null, CancellationTokenSource cancellationTokenSource = null)
        {
            var url = GetUrl("download");
            var map = Map.New.Add("path", serverPath).Add("md5", md5);
            return SysRuntime.Api.DownloadPost(url, map, localPath, progress, cancellationTokenSource);
        }

        /// <summary>
        /// 删除文件
        /// </summary>
        /// <param name="serverPath">服务端文件路径</param>
        /// <returns>成功返回true</returns>
        public BoolMessage Delete(string serverPath)
        {
            var url = GetUrl(SysConst.ApiDeleteName);
            var map = Map.New.Add("path", serverPath);
            return SysRuntime.Api.PostAsMessage(url, map);
        }
    }
}
